package cn.xopencode.userservice.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.mybatis.core.util.DeptUtil;
import cn.xopencode.userservice.convert.MatterConver;
import cn.xopencode.userservice.dal.mysql.dataobject.MatterDO;
import cn.xopencode.userservice.dal.mysql.mapper.MatterMapper;
import cn.xopencode.userservice.dal.mysql.mapper.UserMapper;
import cn.xopencode.userservice.manager.DeptManager;
import cn.xopencode.userservice.rpc.dept.dto.DeptTypeDTO;
import cn.xopencode.userservice.rpc.matter.dto.CreateMatterDTO;
import cn.xopencode.userservice.rpc.matter.dto.MatterDTO;
import cn.xopencode.userservice.rpc.matter.dto.MatterPageDTO;
import cn.xopencode.userservice.rpc.matter.dto.UpdateMatterDTO;
import cn.xopencode.userservice.util.FunUtil;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author xiaodawei
 * @since 2021/9/22
 */
@Service
public class MatterService {


    @Autowired
    private MatterMapper matterMapper;
    @Autowired
    private DeptManager deptManager;
    @Autowired
    private UserMapper userMapper;

    public MatterDTO getMatter(String matterId) {
        MatterDTO matterDTO = null;
        MatterDO matterDO = matterMapper.selectById(matterId);
        if (null != matterDO) {
            matterDTO = MatterConver.INSTANCE.convert(matterDO);
            matterDTO.setEmplName(userMapper.selectById(matterDO.getCreatorId()).getEmplName());
        }
        return matterDTO;
    }


    public String CreateMatter(CreateMatterDTO createMatterDTO) {
        MatterDO matterDO = MatterConver.INSTANCE.convert(createMatterDTO);
        DeptTypeDTO deptTypeDTO = deptManager.getDeptTypeDTO(createMatterDTO.getUserId());
        matterDO.setDeptTypeName1(deptTypeDTO.getDeptTypeName1());
        matterDO.setDeptTypeName2(deptTypeDTO.getDeptTypeName2());
        matterDO.setDeptTypeName3(deptTypeDTO.getDeptTypeName3());
        matterDO.setId(IdUtil.objectId());
        FunUtil.defaultValue(matterDO, createMatterDTO.getUserId(), true);
        matterMapper.insert(matterDO);
        return matterDO.getId();
    }


    public PageResult<MatterDTO> findMatterList(MatterPageDTO matterPageDTO) {
        if (StrUtil.isNotBlank(matterPageDTO.getEmplName())) {
            matterPageDTO.setEmplName(SqlUtils.concatLike(matterPageDTO.getEmplName(), SqlLike.DEFAULT));
        }
        DeptUtil.addPercentSign(matterPageDTO);
        Page<MatterDTO> matterList = matterMapper.findMatterList(new Page<MatterDTO>(matterPageDTO.getPageNo(), matterPageDTO.getPageSize()), matterPageDTO);
        PageResult<MatterDTO> pageResult = new PageResult().setList(matterList.getRecords()).setTotal(matterList.getTotal());
        return pageResult;
    }

    public Integer updateMatter(List<UpdateMatterDTO> updateMatterDTOList) {
        Integer count = 0;
        for (UpdateMatterDTO updateMatterDTO : updateMatterDTOList) {
            MatterDO matterDO = MatterConver.INSTANCE.convert(updateMatterDTO);
            FunUtil.defaultValue(matterDO, updateMatterDTO.getUserId(), false);
            count += matterMapper.updateById(matterDO);
        }
        return count;
    }
}
